package com.chenhy.springbootvuesmbms.controller;

import com.chenhy.springbootvuesmbms.pojo.Customer;
import com.chenhy.springbootvuesmbms.pojo.User;
import com.chenhy.springbootvuesmbms.service.CustomerService;
import com.chenhy.springbootvuesmbms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private UserService userService;

    @GetMapping("/customerList")
    public String queryAllUser(Model model){
        List<Customer> customers = customerService.queryAllCustomer();
        model.addAttribute("customerList", customers);
        return "customer/customerList";
    }

    @GetMapping("/customerDetails")
    public String toCustomerDetails(int id, Model model){
        Customer customer = customerService.queryCustomerById(id);
        model.addAttribute("customer", customer);
        return "customer/customerDetails";
    }

    @GetMapping("/fuzzyQueryCustomer")
    public String fuzzyQueryCustomer(String condition, Model model){
        List<Customer> customers = customerService.fuzzyBillQuery(condition);
        model.addAttribute("customerList", customers);
        return "customer/customerList.html::customerSpace";
    }

    @GetMapping("/addCustomer")
    public String toAddCustomer(Model model){
        List<User> users = userService.queryAllUser();
        model.addAttribute("userList", users);
        return "customer/addCustomer";
    }

    @PostMapping("/addCustomer")
    public String addCustomer(Customer customer, Model model){
        customerService.addCustomer(customer);
        model.addAttribute("msg", "添加订单成功");
        return "customer/addCustomer";
    }

    @GetMapping("/updateCustomer")
    public String toUpdateCustomer(int id, Model model){
        Customer customer = customerService.queryCustomerById(id);
        List<User> users = userService.queryAllUser();
        model.addAttribute("userList", users);
        model.addAttribute("customer", customer);
        return "customer/updateCustomer";
    }

    @PostMapping("/updateCustomer")
    public String updateCustomer(Customer customer, Model model){
        customerService.updateCustomer(customer);
        model.addAttribute("msg", "修改成功");
        return "customer/updateCustomer";

    }

    @PostMapping("/deleteCustomer")
    public String deleteCustomer(int id){
        customerService.deleteCustomer(id);
        return "redirect:/customer/customerList";
    }


}
